package com.unbox.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import com.unbox.admin.entity.CLog;
import com.unbox.admin.mapper.LogMapper;
import com.unbox.admin.service.ILogService;
import com.unbox.common.table.UnBoxPage;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

/**
 * <p>
 * <日志管理业务实现类
 * </p>
 *
 * @author 王振宇
 * @since 2021-04-08
 */
@Service
public class LogServiceImpl extends ServiceImpl<LogMapper, CLog> implements ILogService {

    @Override
    public CLog get(CLog log) {
        LambdaQueryWrapper<CLog> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (!ObjectUtils.isEmpty(log.getId())) {
            lambdaQueryWrapper.eq(CLog::getId, log.getId());
        }
        if (!ObjectUtils.isEmpty(log.getCreateTime())) {
            lambdaQueryWrapper.eq(CLog::getCreateTime, log.getCreateTime());
        }
        if (!StringUtils.isEmpty(log.getMethod())) {
            lambdaQueryWrapper.eq(CLog::getMethod, log.getMethod());
        }
        if (!StringUtils.isEmpty(log.getName())) {
            lambdaQueryWrapper.eq(CLog::getName, log.getName());
        }
        if (!StringUtils.isEmpty(log.getOperationType())) {
            lambdaQueryWrapper.eq(CLog::getOperationType, log.getOperationType());
        }
        if (!ObjectUtils.isEmpty(log.getOperatorId())) {
            lambdaQueryWrapper.eq(CLog::getOperatorId, log.getOperatorId());
        }
        if (!StringUtils.isEmpty(log.getOperatorName())) {
            lambdaQueryWrapper.eq(CLog::getOperatorName, log.getOperatorName());
        }
        if (!StringUtils.isEmpty(log.getRequestBody())) {
            lambdaQueryWrapper.eq(CLog::getRequestBody, log.getRequestBody());
        }
        if (!StringUtils.isEmpty(log.getResponseBody())) {
            lambdaQueryWrapper.eq(CLog::getResponseBody, log.getResponseBody());
        }
        if (!ObjectUtils.isEmpty(log.getRunTime())) {
            lambdaQueryWrapper.eq(CLog::getRunTime, log.getRunTime());
        }
        if (!StringUtils.isEmpty(log.getServiceName())) {
            lambdaQueryWrapper.eq(CLog::getServiceName, log.getServiceName());
        }
        if (!ObjectUtils.isEmpty(log.getStats())) {
            lambdaQueryWrapper.eq(CLog::getStats, log.getStats());
        }
        if (!StringUtils.isEmpty(log.getUrl())) {
            lambdaQueryWrapper.eq(CLog::getUrl, log.getUrl());
        }
       return super.getOne(lambdaQueryWrapper);
    }

    @Override
    public List<CLog> getList(CLog log) {
        LambdaQueryWrapper<CLog> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (!ObjectUtils.isEmpty(log.getId())) {
            lambdaQueryWrapper.eq(CLog::getId, log.getId());
        }
        if (!ObjectUtils.isEmpty(log.getCreateTime())) {
            lambdaQueryWrapper.eq(CLog::getCreateTime, log.getCreateTime());
        }
        if (!StringUtils.isEmpty(log.getMethod())) {
            lambdaQueryWrapper.like(CLog::getMethod, log.getMethod());
        }
        if (!StringUtils.isEmpty(log.getName())) {
            lambdaQueryWrapper.like(CLog::getName, log.getName());
        }
        if (!StringUtils.isEmpty(log.getOperationType())) {
            lambdaQueryWrapper.eq(CLog::getOperationType, log.getOperationType());
        }
        if (!ObjectUtils.isEmpty(log.getOperatorId())) {
            lambdaQueryWrapper.eq(CLog::getOperatorId, log.getOperatorId());
        }
        if (!StringUtils.isEmpty(log.getOperatorName())) {
            lambdaQueryWrapper.eq(CLog::getOperatorName, log.getOperatorName());
        }
        if (!StringUtils.isEmpty(log.getRequestBody())) {
            lambdaQueryWrapper.eq(CLog::getRequestBody, log.getRequestBody());
        }
        if (!StringUtils.isEmpty(log.getResponseBody())) {
            lambdaQueryWrapper.eq(CLog::getResponseBody, log.getResponseBody());
        }
        if (!ObjectUtils.isEmpty(log.getRunTime())) {
            lambdaQueryWrapper.eq(CLog::getRunTime, log.getRunTime());
        }
        if (!StringUtils.isEmpty(log.getServiceName())) {
            lambdaQueryWrapper.eq(CLog::getServiceName, log.getServiceName());
        }
        if (!ObjectUtils.isEmpty(log.getStats())) {
            lambdaQueryWrapper.eq(CLog::getStats, log.getStats());
        }
        if (!StringUtils.isEmpty(log.getUrl())) {
            lambdaQueryWrapper.eq(CLog::getUrl, log.getUrl());
        }
        return super.list(lambdaQueryWrapper);
    }

    @Override
    public UnBoxPage<List<CLog>> getPageList(CLog log, int pageNo, int pageSize) {
        LambdaQueryWrapper<CLog> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (!ObjectUtils.isEmpty(log.getId())) {
            lambdaQueryWrapper.eq(CLog::getId, log.getId());
        }
        if (!ObjectUtils.isEmpty(log.getCreateTime())) {
            lambdaQueryWrapper.eq(CLog::getCreateTime, log.getCreateTime());
        }
        if (!StringUtils.isEmpty(log.getMethod())) {
            lambdaQueryWrapper.like(CLog::getMethod, log.getMethod());
        }
        if (!StringUtils.isEmpty(log.getName())) {
            lambdaQueryWrapper.like(CLog::getName, log.getName());
        }
        if (!StringUtils.isEmpty(log.getOperationType())) {
            lambdaQueryWrapper.eq(CLog::getOperationType, log.getOperationType());
        }
        if (!ObjectUtils.isEmpty(log.getOperatorId())) {
            lambdaQueryWrapper.eq(CLog::getOperatorId, log.getOperatorId());
        }
        if (!StringUtils.isEmpty(log.getOperatorName())) {
            lambdaQueryWrapper.eq(CLog::getOperatorName, log.getOperatorName());
        }
        if (!StringUtils.isEmpty(log.getRequestBody())) {
            lambdaQueryWrapper.eq(CLog::getRequestBody, log.getRequestBody());
        }
        if (!StringUtils.isEmpty(log.getResponseBody())) {
            lambdaQueryWrapper.eq(CLog::getResponseBody, log.getResponseBody());
        }
        if (!ObjectUtils.isEmpty(log.getRunTime())) {
            lambdaQueryWrapper.eq(CLog::getRunTime, log.getRunTime());
        }
        if (!StringUtils.isEmpty(log.getServiceName())) {
            lambdaQueryWrapper.eq(CLog::getServiceName, log.getServiceName());
        }
        if (!ObjectUtils.isEmpty(log.getStats())) {
            lambdaQueryWrapper.eq(CLog::getStats, log.getStats());
        }
        if (!StringUtils.isEmpty(log.getUrl())) {
            lambdaQueryWrapper.eq(CLog::getUrl, log.getUrl());
        }
        lambdaQueryWrapper.orderByDesc(CLog::getCreateTime);
        Page<CLog> page = new Page<>(pageNo,pageSize);
        IPage<CLog> pageResult = this.baseMapper.selectPage(page, lambdaQueryWrapper);
        UnBoxPage<List<CLog>> unBoxPage = new UnBoxPage<>();
        unBoxPage.setPageNo(pageNo);
        unBoxPage.setPageSize(pageSize);
        unBoxPage.setSumPageCount(pageResult.getPages());
        unBoxPage.setSumDataCount(pageResult.getTotal());
        unBoxPage.setDatas(pageResult.getRecords());
        return unBoxPage;
    }

    @Override
    public List<CLog> getIdsList(List<Integer> ids) {
        LambdaQueryWrapper<CLog> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(CLog::getId, ids);
        return super.list(lambdaQueryWrapper);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean save(CLog entity) {
        return super.save(entity);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean saveBatch(List<CLog> entityList) {
        return super.saveBatch(entityList);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean update(CLog log) {
        LambdaQueryWrapper<CLog> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CLog::getId, log.getId());
        return super.update(log, lambdaQueryWrapper);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean updateBatchById(List<CLog> entityList) {
        return super.updateBatchById(entityList);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean remove(CLog log) {
        LambdaQueryWrapper<CLog> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CLog::getId, log.getId());
        return super.remove(lambdaQueryWrapper);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean removeByIds(List<Integer> idList) {
        return super.removeByIds(idList);
    }
}
